//
//  SecurityCheckVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/11/16.
//
// 【首页-电子台账】营业网点安全防范检查表

import Foundation
import UIKit

class SecurityCheckVC: BaseFormVC {
    let listV = SecurityCheckView()
    
    // MARK: - Override
    override func reloadData() {
        listV.deptName = deptName ?? ""
        listV.registerField(withValidator: validator)
    }
    
    override func didValidated(withMode mode: String?) {
        // 完成校验
        
        var isValueFalse = false
        var valPam = ElecParam(bookType: modal.typeValue)
        valPam.tjrq = Date.dateToUp(listV.val33TF.value ?? "")
        if listV.val0TF.isCheck != nil {
            valPam.attrValue0 = listV.val0TF.isCheck! ? "1" : "0"
            if valPam.attrValue0 == "0" {
                isValueFalse = true
            }
        }
        if listV.val1TF.isCheck != nil {
            valPam.attrValue1 = listV.val1TF.isCheck! ? "1" : "0"
            if valPam.attrValue1 == "0" {
                isValueFalse = true
            }
        }
        if listV.val2TF.isCheck != nil {
            valPam.attrValue2 = listV.val2TF.isCheck! ? "1" : "0"
            if valPam.attrValue2 == "0" {
                isValueFalse = true
            }
        }
        if listV.val3TF.isCheck != nil {
            valPam.attrValue3 = listV.val3TF.isCheck! ? "1" : "0"
            if valPam.attrValue3 == "0" {
                isValueFalse = true
            }
        }
        if listV.val4TF.isCheck != nil {
            valPam.attrValue4 = listV.val4TF.isCheck! ? "1" : "0"
            if valPam.attrValue4 == "0" {
                isValueFalse = true
            }
        }
        if listV.val5TF.isCheck != nil {
            valPam.attrValue5 = listV.val5TF.isCheck! ? "1" : "0"
            if valPam.attrValue5 == "0" {
                isValueFalse = true
            }
        }
        if listV.val6TF.isCheck != nil {
            valPam.attrValue6 = listV.val6TF.isCheck! ? "1" : "0"
            if valPam.attrValue6 == "0" {
                isValueFalse = true
            }
        }
        if listV.val7TF.isCheck != nil {
            valPam.attrValue7 = listV.val7TF.isCheck! ? "1" : "0"
            if valPam.attrValue7 == "0" {
                isValueFalse = true
            }
        }
        if listV.val8TF.isCheck != nil {
            valPam.attrValue8 = listV.val8TF.isCheck! ? "1" : "0"
            if valPam.attrValue8 == "0" {
                isValueFalse = true
            }
        }
        if listV.val9TF.isCheck != nil {
            valPam.attrValue9 = listV.val9TF.isCheck! ? "1" : "0"
            if valPam.attrValue9 == "0" {
                isValueFalse = true
            }
        }
        if listV.val10TF.isCheck != nil {
            valPam.attrValue10 = listV.val10TF.isCheck! ? "1" : "0"
            if valPam.attrValue10 == "0" {
                isValueFalse = true
            }
        }
        if listV.val11TF.isCheck != nil {
            valPam.attrValue11 = listV.val11TF.isCheck! ? "1" : "0"
            if valPam.attrValue11 == "0" {
                isValueFalse = true
            }
        }
        if listV.val12TF.isCheck != nil {
            valPam.attrValue12 = listV.val12TF.isCheck! ? "1" : "0"
            if valPam.attrValue12 == "0" {
                isValueFalse = true
            }
        }
        if listV.val13TF.isCheck != nil {
            valPam.attrValue13 = listV.val13TF.isCheck! ? "1" : "0"
            if valPam.attrValue13 == "0" {
                isValueFalse = true
            }
        }
        if listV.val14TF.isCheck != nil {
            valPam.attrValue14 = listV.val14TF.isCheck! ? "1" : "0"
            if valPam.attrValue14 == "0" {
                isValueFalse = true
            }
        }
        if listV.val15TF.isCheck != nil {
            valPam.attrValue15 = listV.val15TF.isCheck! ? "1" : "0"
            if valPam.attrValue15 == "0" {
                isValueFalse = true
            }
        }
        if listV.val16TF.isCheck != nil {
            valPam.attrValue16 = listV.val16TF.isCheck! ? "1" : "0"
            if valPam.attrValue16 == "0" {
                isValueFalse = true
            }
        }
        if listV.val17TF.isCheck != nil {
            valPam.attrValue17 = listV.val17TF.isCheck! ? "1" : "0"
            if valPam.attrValue17 == "0" {
                isValueFalse = true
            }
        }
        if listV.val18TF.isCheck != nil {
            valPam.attrValue18 = listV.val18TF.isCheck! ? "1" : "0"
            if valPam.attrValue18 == "0" {
                isValueFalse = true
            }
        }
        if listV.val19TF.isCheck != nil {
            valPam.attrValue19 = listV.val19TF.isCheck! ? "1" : "0"
            if valPam.attrValue19 == "0" {
                isValueFalse = true
            }
        }
        if listV.val20TF.isCheck != nil {
            valPam.attrValue20 = listV.val20TF.isCheck! ? "1" : "0"
            if valPam.attrValue20 == "0" {
                isValueFalse = true
            }
        }
        if listV.val21TF.isCheck != nil {
            valPam.attrValue21 = listV.val21TF.isCheck! ? "1" : "0"
            if valPam.attrValue21 == "0" {
                isValueFalse = true
            }
        }
        if listV.val22TF.isCheck != nil {
            valPam.attrValue22 = listV.val22TF.isCheck! ? "1" : "0"
            if valPam.attrValue22 == "0" {
                isValueFalse = true
            }
        }
        if listV.val23TF.isCheck != nil {
            valPam.attrValue23 = listV.val23TF.isCheck! ? "1" : "0"
            if valPam.attrValue23 == "0" {
                isValueFalse = true
            }
        }
        if listV.val24TF.isCheck != nil {
            valPam.attrValue24 = listV.val24TF.isCheck! ? "1" : "0"
            if valPam.attrValue24 == "0" {
                isValueFalse = true
            }
        }
        if listV.val25TF.isCheck != nil {
            valPam.attrValue25 = listV.val25TF.isCheck! ? "1" : "0"
            if valPam.attrValue25 == "0" {
                isValueFalse = true
            }
        }
        if listV.val26TF.isCheck != nil {
            valPam.attrValue26 = listV.val26TF.isCheck! ? "1" : "0"
            if valPam.attrValue26 == "0" {
                isValueFalse = true
            }
        }
        if listV.val27TF.isCheck != nil {
            valPam.attrValue27 = listV.val27TF.isCheck! ? "1" : "0"
            if valPam.attrValue27 == "0" {
                isValueFalse = true
            }
        }
        if listV.val28TF.isCheck != nil {
            valPam.attrValue28 = listV.val28TF.isCheck! ? "1" : "0"
            if valPam.attrValue28 == "0" {
                isValueFalse = true
            }
        }
        if listV.val29TF.isCheck != nil {
            valPam.attrValue29 = listV.val29TF.isCheck! ? "1" : "0"
            if valPam.attrValue29 == "0" {
                isValueFalse = true
            }
        }
        if listV.val30TF.isCheck != nil {
            valPam.attrValue30 = listV.val30TF.isCheck! ? "1" : "0"
            if valPam.attrValue30 == "0" {
                isValueFalse = true
            }
        }
        valPam.attrValue31 = listV.val31TV.value
        valPam.attrValue32 = listV.val32TV.value
        valPam.attrValue33 = Date.dateToUp(listV.val33TF.value ?? "")
        valPam.attrValue34 = listV.val34TF.value
        
        if isValueFalse == true && (valPam.attrValue31 == "" || valPam.attrValue32 == "") {
            self.view.showToast(witMsg: "请输入「备注」和「发现问题及整改情况」")
            return
        }
        
        view.showToastActivity()
        do {
            var jsonParam = CommonJsonParam()
            let data: Data = try JSONEncoder().encode(valPam)
            let string = String(data: data, encoding: String.Encoding.utf8)
            jsonParam.jsonStr = string
            API.postStandingBookSave(withJson: jsonParam) { responseModel in
                self.view.hideToastActivity()
                if responseModel.errorCode == .Success {
                    self.view.showToast(witMsg: responseModel.errorMessage)
                    self.navigationController?.popViewController(animated: true)
                }
            }
        } catch {
            self.view.hideToastActivity()
            print("InfraredArmVC didValidated error", error)
        }
    }

    
    override func setupUI() {
        listV.submitBtn.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
        view.addSubview(listV)
        listV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}
